﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraContorller : BaseManager<CameraContorller>
{
    GameObject mainCamera;
    Camera camera;
    float lookCamera;
    Vector3 cameraPos;
    public float LookCamera { get => lookCamera; set => lookCamera = value; }

    public void CreatCamera()
    {
        mainCamera = new GameObject("MainCamera");
        mainCamera.AddComponent<Camera>();
        if (mainCamera.TryGetComponent<Camera>(out camera))
        {
            mainCamera.transform.tag = "MainCamera";
            camera.orthographic = false;//设置透视模式;
            camera.fieldOfView = 13f;//视距
            mainCamera.transform.position = new Vector3(0, 30, -10);
            float xiebian = Vector3.Distance(new Vector3(0, 0, 0), new Vector3(0, 30, -10));
            LookCamera = Mathf.Asin(30 / xiebian) * Mathf.Rad2Deg;
        }
        else
        {
            Debug.LogError($"CameraContorller未找到摄像机组件");
        }
        MonoMgr.GetInstance().AddUpdateListener(UpdateCameraPosition);
        cameraPos = mainCamera.transform.position;//
    }
    public void LookAtPlayer(Vector3 vec)
    {
        camera.transform.LookAt(vec);
    }
    void UpdateCameraPosition()
    {
        if (PlayerController.GetInstance().Player != null)
            mainCamera.transform.position = PlayerController.GetInstance().Player.Obj.transform.position + cameraPos;
    }

    public void Reset()
    {
        mainCamera.transform.position = new Vector3(0, 30, -10);
        cameraPos = mainCamera.transform.position;

    }
}